<?php
namespace app\mobile\controller;
use cmf\controller\HomeBaseController;
use EasyWeChat\Factory;
use think\facade\Env;
use think\Db;

class WechatController extends HomeBaseController
{
	protected $app;
	protected $wx_user;
	private $currentUrl;
	protected $user_id; //ThinkCMF User ID 与$wx_user 无关
//	public function initialize(){
//		//判断用户是否登陆
//		$this->user_id=cmf_get_current_user_id();
//        if(!$this->user_id){
//            $this->redirect('/mobile/user/login');
//        }
//		$this->currentUrl = urlencode($this->request->url(true));
//		$config=[
//			'app_id'=>'wxd65c593552d40442',
//			'secret'=>'99bc505a01f458c459590aa1b1875aec',
//			'token'=>'',
//			'aes_key'=>'',
//			'response_type'=>'array',
//			'log'=>[
//				'level'=>'debug',
//				'file'=>Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR.'wechat.log'
//			],
//			'oauth'=>[
//				'scopes'=>['snsapi_base'],
//				'callback'=>'http://rzjj.coolaring.com/mobile/wechat/oauth_callback?target='.$this->currentUrl,
//			],
//
//		];
//		$this->app = Factory::officialAccount($config);
//		$this->assign('app',$this->app);
//	}

	/** 2.0*/
//    public function initialize(){
//        //判断用户是否登陆
//        $this->user_id=cmf_get_current_user_id();
//        $this->currentUrl = urlencode($this->request->url());
//        $config=[
//            'app_id'=>'wxd65c593552d40442',
//            'secret'=>'99bc505a01f458c459590aa1b1875aec',
//            'token'=>'',
//            'aes_key'=>'',
//            'response_type'=>'array',
//            'log'=>[
//                'level'=>'debug',
//                'file'=>Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR.'wechat.log'
//            ],
//            'oauth'=>[
//                'scopes'=>['snsapi_base'],
//                'callback'=>'http://rzjj.coolaring.com/mobile/wechat/oauth_callback?target='.$this->currentUrl,
//            ],
//
//        ];
//        $this->app = Factory::officialAccount($config);
//        $this->assign('app',$this->app);
//        $oauth = $this->app->oauth;
//        $openid = session('openid');
//        if($openid){
//            $user=Db::name('user')->where('openid',$openid)->find();
//            if($user){
//                unset($user['user_pass']);
//                session('user',$user);
//            }else{
//                if($this->user_id){
//                    Db::name('user')->where('id',$this->user_id)->update(['openid'=>$openid]);
//                }else{
//                    $this->redirect('/mobile/user/login');
//                }
//            }
//        }else{
//            $oauth->scopes(['snsapi_base'])->redirect()->send();
//        }
//    }


	public function oauth_callback(){
	    $config=[
            'app_id'=>'wxd65c593552d40442',
            'secret'=>'99bc505a01f458c459590aa1b1875aec',
        ];
        $app = Factory::officialAccount($config);
		$user = $app->oauth->user();
        session('openid',$user->id);
        $target_url = $this->request->param('target','');
        //$targetUrl = empty($target_url) ? '/mobile/':urldecode($target_url);
        $targetUrl='/mobile/';
        header('location:'.$targetUrl);
       
	}
    public function index()
    {
        return $this->fetch();
    }

}
